package com.example.demo.controller;

import com.example.demo.pojo.Competitions.Competitions;
import com.example.demo.pojo.Competitions.UserCompetitionDetail;
import com.example.demo.pojo.R;
import com.example.demo.service.CompetitionService;
import com.example.demo.service.UserService;
import com.example.demo.utils.TokenUtil.JwtUtils;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Competitions")
public class CompetitionController {

    private static final Logger logger = LoggerFactory.getLogger(CompetitionController.class);

    @Autowired
    private CompetitionService competitionService;

    @Resource
    private UserService userService;


    /**
     * 用户报名比赛
     *
     * @param token token
     * @param id    比赛id
     */
    @PostMapping("/joinCompetition")
    public R<T> joinCompetition(@CookieValue(name = "token", required = false) String token, @RequestParam Integer id, @RequestParam String passcode) {
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("用户未登录或Token无效");
            return R.failed("用户未登录或登录异常");
        }
        try {
            return competitionService.joinCompetition(token, id, passcode);
        } catch (Exception e) {
            logger.error("用户报名比赛失败", e);
            return R.failed("报名比赛失败");
        }
    }

    /**
     * 判断比赛是否截止
     *
     * @param id 比赛id
     */
    @PostMapping("/proveCompetitionById")
    public void proveCompetition(@RequestParam Integer id) {
        try {
            competitionService.proveCompetition(id);
        } catch (Exception e) {
            logger.error("判断比赛失败", e);
        }
    }

    /**
     * 根据比赛id获取比赛
     * @param id 比赛id
     * @return pojo
     */
    @GetMapping("/getCompetitionsById")
    public R<Competitions> getCompetitionsById(@RequestParam int id){
        try {
            return competitionService.getCompetitionsById(id);
        } catch (Exception e) {
            logger.error("获取比赛失败", e);
            return R.failed("获取比赛失败");
        }
    }
    /**
     * 获取比赛接口
     *
     * @param token    token
     * @param pageNum  页码
     * @param pageSize 每页条数
     * @return 比赛集合
     */
    @GetMapping("/getAllCompetition")
    public R<Map<String, Object>> getAllCompetition(@CookieValue(name = "token", required = false) String token
            , @RequestParam int pageNum, @RequestParam int pageSize) {

        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("Token为空");
            return R.failed("未登录或登录异常");
        }

        try {
            return userService.getAllCompetition(pageNum, pageSize);
        } catch (Exception e) {
            logger.error("错误！", e);
            return R.failed("获取比赛失败");
        }
    }

    /**
     * 获取用户已报名比赛
     * @param token token
     * @return 已报名比赛集合
     */
    @GetMapping("/getUserCompetitions")
    public R<List<UserCompetitionDetail>> getUserCompetitions(@CookieValue(name = "token", required = false) String token){
        if (token == null || token.isEmpty() || !JwtUtils.verifyToken(token)) {
            logger.warn("Token为空");
            return R.failed("未登录或登录异常");
        }

        try {
            return competitionService.getUserCompetitions(token);
        } catch (Exception e) {
            logger.error("错误！", e);
            return R.failed("获取比赛失败");
        }
    }

    /**
     * 通过比赛名称获取比赛接口
     *
     * @param competitionsName 比赛名称
     * @return 比赛
     */
    @GetMapping("/getCompetitionsByCompetitionsName")
    public R<List<Competitions>> getCompetitionsByCompetitionsName(@RequestParam String competitionsName){
        try {
            return competitionService.getCompetitionsByCompetitionsName(competitionsName);
        } catch (Exception e) {
            logger.error("错误！", e);
            return R.failed("获取比赛失败");
        }
    }
}
